#include <bits/stdc++.h>
using namespace std;

#define int int64_t
#define endl "\n"
using PII = pair<int, int>;
using TII = tuple<int, int, int>;
#define FOR(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FOR2(i, a, b, c) for (int i = (int)(a); i <= (int)(b); i += c)
#define FORD(i, b, a) for (int i = (int)(a); i >= (int)(b); i--)
#define FORD2(i, b, a, c) for (int i = (int)(a); i >= (int)(b); i -= c)
#define BSI basic_string<int>
#define BSPI basic_string<PII>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define VI vector<int>
#define VII vector<vector<int>>
#define VPII vector<PII>
#define lowbit(x) ((x)&(-x))
#define RE return;
#define RET return true;
#define REF return false;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;
#define pb push_back
#define fi first
#define se second
#define sz size()
constexpr int N = 2e5 + 10;
constexpr int mod = 998244353;

int __FAST_IO__ = [](){
    ios::sync_with_stdio(0), cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(12);
    return 0;
}();

int n;
string s;

void solve() {
    cin>>n>>s;

    vector<vector<int>>pos(26);

    FOR(i,0,n-1){
        pos[s[i]-'a'].emplace_back(i);
    }

    VI id(26);

    iota(ALL(id),0);
    sort(ALL(id),[&](int a,int b){
        return pos[a].sz>pos[b].sz;
    });

    int res=0;
    string ans;

    FOR(i,1,26){
        if(n%i!=0)continue;
        int x=n/i,md=0;
        FOR(j,0,i-1){
            md+=min<int>(x,pos[id[j]].sz);
        }
        if(md>res){
            vector<char>ppp;
            res=md;
            ans=string(n,' ');
            FOR(j,0,i-1){
                FOR(k,0,j/i-1){
                    if(k<pos[id[j]].sz){
                        ans[pos[id[j]][k]]=id[j]+'a';
                    }else{
                        ppp.pb(id[j]+'a');
                    }
                }
            }
            for(auto& x:ans){
                if(x==' '){
                    x=ppp.back();
                    ppp.pop_back();
                }
            }
        }
    }
    cout<<n-res<<endl;
    cout<<ans<<endl;
}

signed main() {
    int Task = 1;
    for (cin >> Task; Task; Task--) {
        solve();
    }
    return 0;
}
